package com.yo.core
{
	import com.yo.manager.ProfilerManager;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageDisplayState;
	import flash.display.StageQuality;
	import flash.display.StageScaleMode;
	import flash.events.ContextMenuEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.net.LocalConnection;
	import flash.system.System;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	
	import org.aswing.KeyboardManager;
	
	public class Game extends Sprite
	{
		protected var _contextMenu:ContextMenu;
		
		/**
		 * 右键菜单 
		 */		
		protected var _captionArr:Array = ["概览", "全屏", "清理"];
		
		public function Game()
		{
			if(stage){
				init();
			}else{
				this.addEventListener(Event.ADDED_TO_STAGE, __addedToStage);
			}
		}
		
		protected function __addedToStage(e:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, __addedToStage);
			init();
		}		
		
		protected function init():void {
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.quality = flash.display.StageQuality.MEDIUM;
			stage.align = StageAlign.TOP_LEFT;
			
			initContextMenu();
			initManager();
			initEvent();
		}
		
		protected function initEvent():void
		{
			this.addEventListener(Event.ENTER_FRAME, __enterFrame);
		}
		
		protected function initManager():void
		{
			ProfilerManager.instance.init(stage);
			ProfilerManager.instance.start();
			ProfilerManager.instance.toggleDisplay();
		}
		
		protected function initContextMenu():void
		{
			_contextMenu = new ContextMenu();
			_contextMenu.hideBuiltInItems();
			_contextMenu.customItems = [];
			
			var len:int = _captionArr.length;
			for (var i:int = 0; i < len; i++) 
			{
				var item:ContextMenuItem = new ContextMenuItem(_captionArr[i], true);
				item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, __menuItemSelect);
				_contextMenu.customItems.push(item);
			}
			
			this.contextMenu = _contextMenu;
		}		
		
		protected function __menuItemSelect(e:ContextMenuEvent):void{
			var item:ContextMenuItem = e.currentTarget as ContextMenuItem;
			switch(item.caption)
			{
				case _captionArr[0]:
					toggleProfilerDisplay();
					break;
				case _captionArr[1]:
					toggleFullScreen();
					break;
				case _captionArr[2]:
					clearMenory();
					break;
				default:
					break;
			}
		}
		
		protected function toggleProfilerDisplay():void
		{
			ProfilerManager.instance.toggleDisplay();
		}
		
		protected function toggleFullScreen():void {
			if(stage.displayState == StageDisplayState.FULL_SCREEN) {
				exitFullScreen();
			}else{
				goScaledFullScreen();
			}
		}
		
		protected function goScaledFullScreen():void
		{
			var rect:Rectangle = new flash.geom.Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
			stage.fullScreenSourceRect = rect;
//            stage.displayState = StageDisplayState.FULL_SCREEN;
			stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
		}
		
		/**
		 * 内存清理
		 */
		public static function clearMenory():void
		{
			try
			{
				new LocalConnection().connect("foo");
				new LocalConnection().connect("foo");
			}
			catch(error : Error){
			
			}
			System.gc();
		}
		
		protected function exitFullScreen():void
		{
			stage.displayState = StageDisplayState.NORMAL;
		}
		
		protected function __enterFrame(event:Event):void
		{
			update();
		}
		
		protected function update():void
		{

		}
	}
}